package student.action;

import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.MyLog;

import edu.student.models.Lookup;
/**
 * 
 * @author Sai Naga Sravani Peraka
 */
public class ModifyLookup extends ActionSupport {
	private int lookup_id=0;
	int i=0;
	private String table="";
	private String name = "";
	private String is_current = "";
	private String interest="";
	private String faculty="";
	private String subject_code="";
	private String duration="";
	private String submit="";
	Lookup lookup =new Lookup();
    private ArrayList<Lookup> allValues = new ArrayList<Lookup>();
    private ArrayList<String> interestList = new ArrayList<String>();
    private ArrayList<Lookup> allInterestList = new ArrayList<Lookup>();
    private ArrayList<Lookup> allFacultyList = new ArrayList<Lookup>();
    private ArrayList<String> facultyList = new ArrayList<String>();

	public String execute() throws Exception {
		Map session = ActionContext.getContext().getSession();
		session = ActionContext.getContext().getSession();
		table=(String)session.get("table");
		MyLog.log("here"+session.get("table"));
     	if (submit.equals("")) {
			MyLog.log("In ModifyLookup.execute().......");
			System.out.println("lookup"+lookup_id);
			lookup = Lookup.findOne(table," where "+table+"_id = " + lookup_id);
			lookup.setTable(table);
			System.out.println("my values"+lookup.getName());
	     	if(table.equals("announcement") || table.equals("news")){
	     		this.allInterestList=lookup.find("interest", "");
	     				
				for (int i = 0; i < allInterestList.size(); i++){
		        	Lookup list = allInterestList.get(i);
		        	interestList.add(list.getName());
		        	lookup.setInterest(lookup.getInterest());
		        	System.out.println("interest"+lookup.getInterest());
		        	MyLog.log("interest name:"+list.getName());
		        }
	     
	     	}
	     	if(table.equals("subject")){
	     		this.allFacultyList=lookup.find("faculty", "");
	     				
				for (int i = 0; i < allFacultyList.size(); i++){
		        	Lookup list = allFacultyList.get(i);
		        	facultyList.add(list.getName());
		        	MyLog.log("faculty name:"+list.getName());
		        }
	     
	     	}
	        	MyLog.log("name:"+lookup.getName());
	        	MyLog.log("iscurrent:"+lookup.getIs_current());
	        	MyLog.log("table:"+lookup.getTable());
			MyLog.log("Lookup_id to be modified is :"+lookup_id);
			return "initial";
		}
	else{
		lookup.setTable(table);
		lookup.setId(lookup_id);
		lookup.setName(name);
		lookup.setIs_current(is_current);
		System.out.println("subject_code"+subject_code);
		if(table.equals("announcement") || table.equals("news")){
			lookup.setInterest(interest);
			lookup.setDuration(duration);
		}
		if(table.equals("semester")){
			lookup.setDuration(duration);
			System.out.println("duration"+duration);
		}
		if(table.equals("subject")){
			lookup.setFaculty(faculty);
			lookup.setSubject_code(subject_code);
		}
		lookup.update(table);
		MyLog.log(lookup_id+" lookup details are successfully updated...!");
		this.allValues = lookup.find(table,"");
		return "success";
		}
	}

	public Lookup getLookup() {
		return lookup;
	}

	public void setLookup(Lookup lookup) {
		this.lookup = lookup;
	}

	public int getLookup_id() {
		return lookup_id;
	}

	public void setLookup_id(int lookup_id) {
		this.lookup_id = lookup_id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIs_current() {
		return is_current;
	}

	public void setIs_current(String is_current) {
		this.is_current = is_current;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public String getTable() {
		return table;
	}

	public void setTable(String table) {
		this.table = table;
	}

	public ArrayList<Lookup> getAllValues() {
		return allValues;
	}

	public void setAllValues(ArrayList<Lookup> allValues) {
		this.allValues = allValues;
	}

	public ArrayList<String> getInterestList() {
		return interestList;
	}

	public void setInterestList(ArrayList<String> interestList) {
		this.interestList = interestList;
	}

	public String getInterest() {
		return interest;
	}

	public void setInterest(String interest) {
		this.interest = interest;
	}

	public String getFaculty() {
		return faculty;
	}

	public void setFaculty(String faculty) {
		this.faculty = faculty;
	}

	public ArrayList<String> getFacultyList() {
		return facultyList;
	}

	public void setFacultyList(ArrayList<String> facultyList) {
		this.facultyList = facultyList;
	}

	public String getSubject_code() {
		return subject_code;
	}

	public void setSubject_code(String subject_code) {
		this.subject_code = subject_code;
	}

	public String getDuration() {
		return duration;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}
}
